/**
 * Copyright (C) 2024-2030 Super BilGen & 超亿时代信息技术股份有限公司.All Rights Reserved.
 */
package cn.com.yang.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import cn.com.yang.utils.DateUtil;
import cn.hutool.core.util.NumberUtil;

/**
 * @Model cn.com.yang.pdf.TestPdfImgs
 * @Desc TODO
 * @author yangyijin
 * @since 2025年5月13日-下午7:45:35
 */
public class TestPdfImg {
	private Logger logger = LoggerFactory.getLogger(getClass());

	public void exec() {
		StringBuffer fileNameSb = new StringBuffer(DateUtil.localTimeFormat("yyyyMMddHHmmss"));
		fileNameSb.append(System.currentTimeMillis());
		String fileName = fileNameSb.toString();
		StringBuffer pdfFilePathSb = new StringBuffer("/app/file/");
		pdfFilePathSb.append(fileName).append(".pdf");
		try {
			File filePathTmp = new File("/app/file/");
			if (!filePathTmp.exists()) {
				filePathTmp.mkdirs();
			}
			// 生成PDF
			ceratePdf(pdfFilePathSb.toString());

		} catch (Exception e) {
			logger.error("pdf创建失败：", e);
		}

	}

	private void ceratePdf(String pdfFilePath) {
		// TODO Auto-generated method stub
		try (FileOutputStream fout = new FileOutputStream(pdfFilePath)) {
			// 创建文档对象
			Document document = new Document(PageSize.A6);
			// 导出的地址
			PdfWriter.getInstance(document, fout);
			// 打开文档
			document.open();
			BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font nameFont = new Font(baseFont, 12, Font.NORMAL);
			nameFont.setColor(BaseColor.BLACK);
			Font commonFont = new Font(baseFont, 8, Font.NORMAL);
			commonFont.setColor(BaseColor.GRAY);
			createHeadTable(document, baseFont);
			createPayeeTable(document, nameFont, commonFont);
			createPayerTable(document, nameFont, commonFont);
			createTranTable(document, baseFont);
			document.close();
		} catch (Exception e) {
			logger.error("pdf创建失败：", e);
		}
	}

	private void createTranTable(Document document, BaseFont baseFont) throws DocumentException {
		Font keyFont = new Font(baseFont, 10, Font.NORMAL);
		keyFont.setColor(BaseColor.GRAY);

		Font valueFont = new Font(baseFont, 10, Font.NORMAL);
		valueFont.setColor(BaseColor.BLACK);
		// 创建表对象
		PdfPTable transTable = new PdfPTable(2);
		transTable.setHorizontalAlignment(Element.ALIGN_CENTER);
		transTable.setWidthPercentage(100); // 宽度100%填充
		float[] headTableColumnWidths = { 0.4f, 0.6f };
		transTable.setWidths(headTableColumnWidths);
		// 设置行对象
		ArrayList<PdfPRow> rows = transTable.getRows();
		// 第一行
		PdfPCell rowCell1[] = new PdfPCell[2];
		rowCell1[0] = new PdfPCell(new Paragraph("用途", keyFont));
		rowCell1[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[0].setMinimumHeight(20);
		rowCell1[0].disableBorderSide(15); // 隐藏边框
		String tranUse = "交易用途";
		rowCell1[1] = new PdfPCell(new Paragraph(tranUse, valueFont));
		rowCell1[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[1].setMinimumHeight(20);
		rowCell1[1].disableBorderSide(15); // 隐藏边框
		// 第二行
		PdfPCell rowCell2[] = new PdfPCell[2];
		rowCell2[0] = new PdfPCell(new Paragraph("转账流水号", keyFont));
		rowCell2[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell2[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell2[0].setMinimumHeight(20);
		rowCell2[0].disableBorderSide(15); // 隐藏边框
		String tranSn = "T93214821941248";
		rowCell2[1] = new PdfPCell(new Paragraph(tranSn, valueFont));
		rowCell2[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell2[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell2[1].setMinimumHeight(20);
		rowCell2[1].disableBorderSide(15); // 隐藏边框
		// 第三行
		PdfPCell rowCell3[] = new PdfPCell[2];
		rowCell3[0] = new PdfPCell(new Paragraph("交易时间", keyFont));
		rowCell3[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell3[0].setMinimumHeight(20);
		rowCell3[0].disableBorderSide(15); // 隐藏边框
		String tranTime = "2025-05-13 18:00:00";
		rowCell3[1] = new PdfPCell(new Paragraph(tranTime, valueFont));
		rowCell3[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell3[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell3[1].setMinimumHeight(20);
		rowCell3[1].disableBorderSide(15); // 隐藏边框
		// 第四行
		PdfPCell rowCell4[] = new PdfPCell[2];
		rowCell4[0] = new PdfPCell(new Paragraph("回单号", keyFont));
		rowCell4[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell4[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell4[0].setMinimumHeight(20);
		rowCell4[0].disableBorderSide(15); // 隐藏边框
		String billCode = "3432423";
		rowCell4[1] = new PdfPCell(new Paragraph(billCode, valueFont));
		rowCell4[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell4[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell4[1].setMinimumHeight(20);
		rowCell4[1].disableBorderSide(15); // 隐藏边框
		// 第五行
		PdfPCell rowCell5[] = new PdfPCell[2];
		rowCell5[0] = new PdfPCell();
		rowCell5[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell5[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell5[0].setMinimumHeight(15);
		rowCell5[0].disableBorderSide(15); // 隐藏边框
		rowCell5[0].setColspan(2);
		// 第六行
		Font tipFont = new Font(baseFont, 8, Font.NORMAL);
		tipFont.setColor(BaseColor.LIGHT_GRAY);
		String tip = "此回单仅供参考，实际到账状态以收款方账户交易记录为准。";
		PdfPCell rowCell6[] = new PdfPCell[2];
		rowCell6[0] = new PdfPCell(new Paragraph(tip, tipFont));
		rowCell6[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell6[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell6[0].setMinimumHeight(25);
		rowCell6[0].disableBorderSide(15); // 隐藏边框
		rowCell6[0].setColspan(2);

		rows.add(new PdfPRow(rowCell1));
		rows.add(new PdfPRow(rowCell2));
		rows.add(new PdfPRow(rowCell3));
		rows.add(new PdfPRow(rowCell4));
		rows.add(new PdfPRow(rowCell5));
		rows.add(new PdfPRow(rowCell6));
		document.add(transTable);
	}

	private void createPayeeTable(Document document, Font nameFont, Font commonFont)
			throws DocumentException, BadElementException, MalformedURLException, IOException {
		// 创建表对象
		PdfPTable payeeTable = new PdfPTable(2);
		payeeTable.setHorizontalAlignment(Element.ALIGN_CENTER);
		payeeTable.setWidthPercentage(100); // 宽度100%填充
		float[] payeeTableColumnWidths = { 0.25f, 0.75f };
		payeeTable.setWidths(payeeTableColumnWidths);

		// 设置行对象
		ArrayList<PdfPRow> rows = payeeTable.getRows();

		String tranType = "1";
		String rcvAcctName = "ddddd";
		Paragraph rcvAcctParagraph = new Paragraph(rcvAcctName + "  ", nameFont);
		String rcvAcctNo = "2132321321";
		String rcvMobile = "13788881111";
		String rcvDrecBankName = "hdsiadsaihfas";
		String maskFlag = "1";

		Paragraph rcvAcctNoParagraph = null;
		if ("02".equals(tranType)) {
			// 手机号转账
			if ("1".equals(maskFlag)) {
				rcvMobile = rcvMobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
			}
			rcvAcctNoParagraph = new Paragraph(rcvMobile, commonFont);
		} else {
			if ("1".equals(maskFlag)) {
				rcvAcctNo = rcvAcctNo.replaceAll("(\\d{4})\\d+(\\d{4})", "$1********$2");
			}
			rcvAcctNoParagraph = new Paragraph(rcvAcctNo, commonFont);
			rcvAcctParagraph.add(new Paragraph(rcvDrecBankName, commonFont));
		}
		// 第一行
		Image imagePayee = Image.getInstance("classpath:image/payee.png");
		imagePayee.scaleToFit(25, 25);
		PdfPCell rowCell1[] = new PdfPCell[2];
		rowCell1[0] = new PdfPCell(imagePayee);
		rowCell1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
		rowCell1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[0].setMinimumHeight(25);
		rowCell1[0].disableBorderSide(15); // 隐藏边框
		rowCell1[0].setRowspan(2);
		rowCell1[0].setBackgroundColor(new BaseColor(253, 249, 246));

		rowCell1[1] = new PdfPCell(rcvAcctParagraph);
		rowCell1[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[1].setMinimumHeight(25);
		rowCell1[1].disableBorderSide(15); // 隐藏边框
		rowCell1[1].setBackgroundColor(new BaseColor(253, 249, 246));
		rows.add(new PdfPRow(rowCell1));

		PdfPCell rowCell2[] = new PdfPCell[2];
		rowCell2[1] = new PdfPCell(rcvAcctNoParagraph);
		rowCell2[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell2[1].setVerticalAlignment(Element.ALIGN_TOP);
		rowCell2[1].setMinimumHeight(20);
		rowCell2[1].disableBorderSide(15); // 隐藏边框
		rowCell2[1].setBackgroundColor(new BaseColor(253, 249, 246));
		rows.add(new PdfPRow(rowCell2));

		PdfPCell rowCell3[] = new PdfPCell[2];
		rowCell3[0] = new PdfPCell(new Paragraph());
		rowCell3[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell3[0].setMinimumHeight(10);
		rowCell3[0].disableBorderSide(15); // 隐藏边框
		rowCell3[0].setColspan(2);
		rows.add(new PdfPRow(rowCell3));

		document.add(payeeTable);
	}

	private void createPayerTable(Document document, Font nameFont, Font commonFont)
			throws DocumentException, BadElementException, MalformedURLException, IOException {
		// 创建表对象
		PdfPTable payerTable = new PdfPTable(2);
		payerTable.setHorizontalAlignment(Element.ALIGN_CENTER);
		payerTable.setWidthPercentage(100); // 宽度100%填充
		float[] payeeTableColumnWidths = { 0.25f, 0.75f };
		payerTable.setWidths(payeeTableColumnWidths);

		// 设置行对象
		ArrayList<PdfPRow> rows = payerTable.getRows();

		String payerAcctName = "xxxxxxx";
		Paragraph payerAcctParagraph = new Paragraph(payerAcctName + "  ", nameFont);

		String payerBankName = "xxxxxxxxx";
		payerAcctParagraph.add(new Paragraph(payerBankName, commonFont));

		String payerAcctNo = "121212121";
		String maskFlag = "1";
		if ("1".equals(maskFlag)) {
			payerAcctNo = payerAcctNo.replaceAll("(\\d{4})\\d+(\\d{4})", "$1********$2");
		}
		Paragraph payerAcctNoParagraph = new Paragraph(payerAcctNo, commonFont);
		// 第一行
		Image imagePayee = Image.getInstance("classpath:image/payer.png");
		imagePayee.scaleToFit(25, 25);
		PdfPCell rowCell1[] = new PdfPCell[2];
		rowCell1[0] = new PdfPCell(imagePayee);
		rowCell1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
		rowCell1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[0].setMinimumHeight(25);
		rowCell1[0].disableBorderSide(15); // 隐藏边框
		rowCell1[0].setRowspan(2);
		rowCell1[0].setBackgroundColor(new BaseColor(246, 250, 253));

		rowCell1[1] = new PdfPCell(payerAcctParagraph);
		rowCell1[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[1].setMinimumHeight(25);
		rowCell1[1].disableBorderSide(15); // 隐藏边框
		rowCell1[1].setBackgroundColor(new BaseColor(246, 250, 253));
		rows.add(new PdfPRow(rowCell1));

		PdfPCell rowCell2[] = new PdfPCell[2];
		rowCell2[1] = new PdfPCell(payerAcctNoParagraph);
		rowCell2[1].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell2[1].setVerticalAlignment(Element.ALIGN_TOP);
		rowCell2[1].setMinimumHeight(20);
		rowCell2[1].disableBorderSide(15); // 隐藏边框
		rowCell2[1].setBackgroundColor(new BaseColor(246, 250, 253));
		rows.add(new PdfPRow(rowCell2));

		PdfPCell rowCell3[] = new PdfPCell[2];
		rowCell3[0] = new PdfPCell(new Paragraph());
		rowCell3[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell3[0].setMinimumHeight(10);
		rowCell3[0].disableBorderSide(15); // 隐藏边框
		rowCell3[0].setColspan(2);
		rows.add(new PdfPRow(rowCell3));

		document.add(payerTable);
	}

	private void createHeadTable(Document document, BaseFont baseFont) throws DocumentException {
		Font titleFont = new Font(baseFont, 14, Font.NORMAL);
		titleFont.setColor(BaseColor.BLACK);

		Font amtFont = new Font(baseFont, 28, Font.NORMAL);
		amtFont.setColor(BaseColor.BLACK);

		// 创建PDF头
		// 创建表对象
		PdfPTable headTable = new PdfPTable(1);
		headTable.setHorizontalAlignment(Element.ALIGN_CENTER);
		headTable.setWidthPercentage(100); // 宽度100%填充
		// 设置行对象
		List<PdfPRow> rows = headTable.getRows();
		PdfPCell rowCell1[] = new PdfPCell[1];
		String payerAcctName = "dsadsadsadasdfsaf";
		StringBuffer titleSb = new StringBuffer(payerAcctName);
		titleSb.append("xccccc");
		Paragraph titleParagraph = new Paragraph(titleSb.toString(), titleFont);
		rowCell1[0] = new PdfPCell(titleParagraph);
		rowCell1[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell1[0].setMinimumHeight(20);
		rowCell1[0].disableBorderSide(15); // 隐藏边框
		rows.add(new PdfPRow(rowCell1));

		PdfPCell rowCell2[] = new PdfPCell[1];
		String tranAmt = "12.00";
		String fmtAmt = NumberUtil.decimalFormatMoney(new BigDecimal(tranAmt).doubleValue());
		Paragraph amtParagraph = new Paragraph("￥" + fmtAmt, amtFont);
		rowCell2[0] = new PdfPCell(amtParagraph);
		rowCell2[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell2[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell2[0].setMinimumHeight(30);
		rowCell2[0].disableBorderSide(15); // 隐藏边框
		rows.add(new PdfPRow(rowCell2));

		PdfPCell rowCell3[] = new PdfPCell[1];
		rowCell3[0] = new PdfPCell(new Paragraph());
		rowCell3[0].setHorizontalAlignment(Element.ALIGN_LEFT);
		rowCell3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
		rowCell3[0].setMinimumHeight(15);
		rowCell3[0].disableBorderSide(15); // 隐藏边框
		rows.add(new PdfPRow(rowCell3));

		document.add(headTable);
	}
}
